package com.ruoyi.basics.controller;

import com.ruoyi.basics.domain.WmsArea;
import com.ruoyi.basics.domain.vo.WmsAreaVo;
import com.ruoyi.basics.service.IWmsAreaService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 库区管理Controller
 * 
 * @author ruoyi
 * @date 2025-06-09
 */
@RestController
@RequestMapping("/basics/area")
public class WmsAreaController extends BaseController
{
    @Autowired
    private IWmsAreaService wmsAreaService;

    /**
     * 查询库区管理列表
     */
    @PreAuthorize("@ss.hasPermi('basics:area:list')")
    @GetMapping("/list")
    public TableDataInfo list(WmsArea wmsArea)
    {
        startPage();
        List<WmsAreaVo> list = wmsAreaService.selectWmsAreaList(wmsArea);
        return getDataTable(list);
    }

    /**
     * 导出库区管理列表
     */
    @PreAuthorize("@ss.hasPermi('basics:area:export')")
    @Log(title = "库区管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WmsArea wmsArea)
    {
        List<WmsAreaVo> list = wmsAreaService.selectWmsAreaList(wmsArea);
        ExcelUtil<WmsAreaVo> util = new ExcelUtil<WmsAreaVo>(WmsAreaVo.class);
        util.exportExcel(response, list, "库区管理数据");
    }

    /**
     * 获取库区管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('basics:area:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(wmsAreaService.selectWmsAreaById(id));
    }

    /**
     * 新增库区管理
     */
    @PreAuthorize("@ss.hasPermi('basics:area:add')")
    @Log(title = "库区管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WmsArea wmsArea)
    {
        return toAjax(wmsAreaService.insertWmsArea(wmsArea));
    }

    /**
     * 修改库区管理
     */
    @PreAuthorize("@ss.hasPermi('basics:area:edit')")
    @Log(title = "库区管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WmsArea wmsArea)
    {
        return toAjax(wmsAreaService.updateWmsArea(wmsArea));
    }

    /**
     * 删除库区管理
     */
    @PreAuthorize("@ss.hasPermi('basics:area:remove')")
    @Log(title = "库区管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(wmsAreaService.deleteWmsAreaByIds(ids));
    }


    /**
     * 库区状态修改
     */
    @PreAuthorize("@ss.hasPermi('basics:area:AreaStatus')")
    @Log(title = "仓库状态修改", businessType = BusinessType.UPDATE)
    @PostMapping("/AreaStatus/{id}/{status}")
    public AjaxResult AreaStatus(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        return toAjax(wmsAreaService.AreaStatus(id, status));
    }


    /**
     * 生成库区编码的接口
     * @return 新生成的库区编码
     */
    @PreAuthorize("@ss.hasPermi('basics:area:add')")

    @GetMapping("/generateAreaCode")
    public String generateAreaCode() {
        return wmsAreaService.generateAreaCode();
    }

    /**
     * 导入
     */
    @PreAuthorize("@ss.hasPermi('basics:area:import')")
    @Log(title = "导入", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult excelImport(MultipartFile file) throws Exception {
        ExcelUtil<WmsAreaVo> util = new ExcelUtil<WmsAreaVo>(WmsAreaVo.class);
        List<WmsAreaVo> wmsAreaVos = util.importExcel(file.getInputStream());
        return toAjax(wmsAreaService.insertWmArea(wmsAreaVos));
    }


}
